package library.view;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import library.dao.Impl.BookTypeDaoImpl;
import library.pojo.Book;
import library.pojo.User;
import library.service.LibraryService;

public class UpdateUserDialog extends JDialog {
	private User user;
	private JTextField usernameTextField;
	private JTextField passwordTextField;
	private JTextField nameTextField;
	private JTextField phoneTextField;
	private JComboBox userTypeComboBox;
	
	public UpdateUserDialog(String title, User user) {
		ImageIcon icon = new ImageIcon(MainJTPane.class.getResource("/title.png"));
		this.setIconImage(icon.getImage());
		this.user = user;
		this.setTitle(title);
		this.add(getUserPanel());
		this.setModal(true);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.validate();
	}

	private JPanel getUserPanel() {
		JPanel userPanel = new JPanel();
		usernameTextField = new JTextField(15);
		passwordTextField = new JTextField(15);
		nameTextField = new JTextField(15);
		phoneTextField = new JTextField(15);
		usernameTextField.setEditable(false);
		usernameTextField.setText(user.getUsername());
		passwordTextField.setText(user.getPassword());
		nameTextField.setText(user.getName());
		phoneTextField.setText(user.getPhone());
		String[] way = { "借阅者", "图书管理员","系统管理员" };
		userTypeComboBox = new JComboBox(way);
		userTypeComboBox.setPreferredSize(new Dimension(170, 22));
		JButton confirmButton = new JButton("确定修改");
		confirmButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateActionPerformed(e);
			}
		});
		userPanel.add(new JLabel("      用户名"));
		userPanel.add(usernameTextField);
		userPanel.add(new JLabel("        密码"));
		userPanel.add(passwordTextField);
		userPanel.add(new JLabel("        姓名"));
		userPanel.add(nameTextField);
		userPanel.add(new JLabel("联系电话"));
		userPanel.add(phoneTextField);
		userPanel.add(new JLabel("用户类型"));
		userPanel.add(userTypeComboBox);
		userPanel.add(confirmButton);
		this.setSize(270, 220);
		return userPanel;
	}

	protected void updateActionPerformed(ActionEvent e) {
		if (this.isEmpty(passwordTextField.getText())) {
			JOptionPane.showMessageDialog(null, "密码不能为空！");
			return;
		}
		if (passwordTextField.getText().length() < 3) {
			JOptionPane.showMessageDialog(null, "密码长度不得小于3位！");
			return;
		}
		if (this.isEmpty(nameTextField.getText())) {
			JOptionPane.showMessageDialog(null, "姓名不能为空！");
			return;
		}
		if (this.isEmpty(phoneTextField.getText())) {
			JOptionPane.showMessageDialog(null, "联系电话不能为空！");
			return;
		}
		if (!phoneTextField.getText().matches("[0-9]{0,11}") || phoneTextField.getText().length() != 11) {
			JOptionPane.showMessageDialog(null, "请输入正确的手机号！");
			return;
		}
		
		LibraryService service = new LibraryService();
		BookTypeDaoImpl bookTypedao = new BookTypeDaoImpl();
		User users = new User(usernameTextField.getText(), passwordTextField.getText(), nameTextField.getText(),
				(String) userTypeComboBox.getSelectedItem(),phoneTextField.getText());
		service.updateUser(users);
		this.dispose();
		JOptionPane.showMessageDialog(null, "修改成功！");
	}

	/**
	 * 判断是否为空
	 */
	private boolean isEmpty(String str) {
		if (str == null || "".equals(str.trim())) {
			return true;
		} else {
			return false;
		}
	}
}
